<template>
  <BasicTable @register="registerTable" @fetchSuccess="onFetchSuccess"
              :indentSize="10" :indexColumnProps="indexColumnProps"
              :onRowClick="handleClick" :onRowDbClick="handleDblclick" :rowClassName="rowClassName"
  >
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--角色用户表格-->
  <RoleUserTable @register="roleUserDrawer" />
  <!--角色编辑抽屉-->
  <RoleDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  <RoleDesc @register="registerDesc"></RoleDesc>
  <!--角色菜单授权抽屉-->
  <RolePermissionDrawer @register="rolePermissionDrawer" />
  <!--角色首页配置-->
  <RoleIndexModal @register="registerIndexModal" />
</template>
<script lang="ts" name="system-role" setup>
  import { nextTick, ref, reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import RoleDrawer from './components/RoleDrawer.vue';
  import RoleDesc from './components/RoleDesc.vue';
  import RolePermissionDrawer from './components/RolePermissionDrawer.vue';
  import RoleIndexModal from './components/RoleIndexModal.vue';
  import RoleUserTable from './components/RoleUserTable.vue';
  import { columns } from './role.data';
  import { treeGrid, deleteRole, batchDeleteRole } from './role.api';
  import { useListPage } from '/@/hooks/system/useListPage';
  const showFooter = ref(true);
  const [roleUserDrawer, { openDrawer: openRoleUserDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerIndexModal, { openModal: openIndexModal }] = useModal();
  const [rolePermissionDrawer, { openDrawer: openRolePermissionDrawer }] = useDrawer();
  const [registerDesc, { openDrawer: openRoleDesc }] = useDrawer();

  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: 'role-template',
    tableProps: {
      title: '角色列表',
      api: treeGrid,
      columns: columns,
      showIndexColumn: true,    //行号
      useSearchForm: false,
      showActionColumn: true,
      showTableSetting: false,
      defaultExpandAllRows: true,
      isTreeTable: true,
      expandIconColumnIndex: 0,
      pagination: false,
      actionColumn: {
        width: 120,
      },
    },
  });
  const [registerTable, { reload, expandAll }, { rowSelection, selectedRowKeys }] = tableContext;

  const indexColumnProps = {
    title: "",
    dataIndex: "index",
    width: "20px"
  };

  /**
   * 新增
   */
  function handleCreate() {
    showFooter.value = true;
    openDrawer(true, {
      isUpdate: false,
    });
  }
  /**
   * 编辑
   */
  function handleEdit(record: Recordable) {
    showFooter.value = true;
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }
  /**
   * 详情
   */
  function handleDetail(record) {
    showFooter.value = false;
    openRoleDesc(true, {
      record,
      isUpdate: true,
    });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteRole({ id: record.id }, reload);
  }

  /**
   * 角色授权弹窗
   */
  function handlePerssion(record) {
    openRolePermissionDrawer(true, { roleId: record.id });
  }

  /**
   * 首页配置弹窗
   */
  function handleIndexConfig(roleCode) {
    openIndexModal(true, { roleCode });
  }

  /**
   * 角色用户
   */
  function handleUser(record) {
    //onSelectChange(selectedRowKeys)
    openRoleUserDrawer(true, record);
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        icon: "ant-design:form-outlined",
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '用户',
        color: "success",
        icon: "ant-design:user-outlined",
        onClick: handleUser.bind(null, record)
      },
      {
        label: '授权',
        color: "warning",
        icon: "ant-design:setting-outlined",
        onClick: handlePerssion.bind(null, record)
      },
      {
        label: "删除",
        color: "error",
        icon: "ant-design:rest-twotone",
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        }
      }
    ];
  }

  function handleSuccess() {
    reload();
  }

  async function onFetchSuccess() {
    // 演示默认展开所有表项
    await nextTick(() => {
      expandAll();
    });
  }

  //单击行
  let selectRows = reactive<any>({id: '', username: ''});
  function handleClick(record) {
    selectRows.id = record.id
  }

  //双击行
  function handleDblclick(record) {
    handleEdit(record);
  }

  function rowClassName(record) {
    return record.id === selectRows.id ? 'row-selected' : '';
  }
</script>
